import random
from django import forms
from chart_web import models
from django.core.exceptions import ValidationError


class kaidan_article(forms.ModelForm):
    '''怪按文章添加校验'''

    class Meta:
        model = models.KaidanRestaurant
        fields = ['name', 'dec', 'content']

    # 当前该用户不能发名字一样的文章
    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request = request

    def clean_name(self):
        name = self.cleaned_data.get('name')
        exist = models.KaidanRestaurant.objects.filter(userinfo=self.request.chart_web.user, name=name).exists()
        if exist:
            raise ValidationError('您当前已经有一篇该名字的文章了！给他换个马甲吧')
        if '傻逼' in name:
            raise ValidationError('当前题目中含有不文明词汇！！！')
        return name


class comment_form(forms.ModelForm):
    '''评论校验'''

    class Meta:
        model = models.UpDownComment
        fields = ['comment']

    def clean_comment(self):
        comment = self.cleaned_data.get('comment')
        if not comment:
            raise ValidationError('评论不能为空！！！')
        if '傻逼' in comment:
            raise ValidationError('当前题目中含有不文明词汇！！！')
        return comment
